<template>
  <el-dialog :title="formData.id?'编辑客户':'新增客户'" :visible="showDialog" :before-close="closeFn">
    <!-- 表单 -->
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="经纪" prop="agent">
        <el-select v-model="formData.agent" placeholder="请选择经纪人">
          <el-option
            v-for="item in agentsList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" style="width:50%" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" style="width:50%" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model="formData.wechat" style="width:50%" placeholder="请输入微信" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <!-- 放置上传图片 -->
        <uploadImage ref="uploadImage" />
      </el-form-item>

    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addClients, editClients, getClientsDetail } from '@/api/client'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    agentsList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      formData: {
        agent: '',
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 2, max: 4, message: '用户姓名为2-4位'
        }],
        phone: [{ required: true, message: '电话不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '电话格式不正确', trigger: 'blur'
        }],
        desc: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
        email: [
          { required: true, message: '经纪邮箱不能为空', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: '请输入正确邮箱格式', trigger: 'blur'
          }
        ],
        agent: [
          { required: true, message: '经纪不能为空', trigger: 'blur' }
        ]

      }
    //   AgentsList: []
    }
  },
  async created() {
    // // 获取经纪列表
    // const res = await getAgentsList()
    // this.AgentsList = res
    // console.log(92, this.AgentsList)
  },
  methods: {
    // 编辑客户
    async getClientsDetail(id) {
      console.log(99, id)
      // 数据回显
      this.formData = await getClientsDetail(id)
      this.formData.agent = this.formData.agent && this.formData.agent.id || ''
      this.$refs.uploadImage.fileList.push({
        url: this.formData.image
      })
    },
    // 添加及编辑客户
    async confirmFn() {
      // 格式校验
      await this.$refs.form.validate()
      //   分析是添加还是编辑
      if (this.formData.id) {
        // 编辑
        // 当图片没有上传成功的时候不允许更新
        if (this.$refs.uploadImage.fileList.length === 0) {
          return this.$message.error('请上传图片!')
        }
        this.formData.image = this.$refs.uploadImage.fileList[this.$refs.uploadImage.fileList.length - 1].url
        await editClients(this.formData)
        this.$message.success('编辑成功')
      } else {
      //   添加
        // 当图片没有上传成功的时候不允许更新
        if (this.$refs.uploadImage.fileList[0] && !this.$refs.uploadImage.fileList[0].url) {
          return this.$message.error('图片还没有上传成功，请稍后!')
        }
        this.formData.image = this.$refs.uploadImage.fileList[0] && this.$refs.uploadImage.fileList[0].url
        // 调用添加接口
        await addClients(this.formData)
        this.$message.success('添加成功')
      }
      //   关闭弹窗
      this.closeFn()
    },
    // 关闭弹窗
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空表单
      this.formData = {
        agent: '',
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      }
      //   清空表单校验
      this.$refs.form.resetFields()
      // 清空图像
      this.$refs.uploadImage.fileList = []
      // 重新获取数据渲染页面
      // this.$parent.getClientsList()
      this.$emit('getClient')
    },
    // 新增客户函数
    async addClients() {
      await addClients(this.formData)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
